//
//  YLJNavigationController.m
//  ACE
//
//  Created by Mickey Yin on 2021/12/1.
//

#import "YLJNavigationController.h"
#import "UIBarButtonItem+YLJExtension.h"

@interface YLJNavigationController ()<UINavigationBarDelegate,UIGestureRecognizerDelegate>

@end

@implementation YLJNavigationController


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return self;
}

+ (void)load {
    [super load];
    
//    UINavigationBar *bar = [UINavigationBar appearance];
//    [self settingNavigationBarTheme:bar];
    
    UIBarButtonItem *item;
    if (@available(iOS 9, *)) {
        item = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]];
    } else {
        item = [UIBarButtonItem appearance];
    }
    [self settingBarButtonItemTheme:item];
}

+ (void)settingBarButtonItemTheme:(UIBarButtonItem *)item {
    //导航栏的BarButtonItem
    
    UIColor *tintColot = rgba(51, 51, 51, 1);
    NSDictionary* textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIFont systemFontOfSize:kAutoFont(14.f)],NSFontAttributeName,
                                    tintColot,NSForegroundColorAttributeName, nil];
    [item setTitleTextAttributes:textAttributes forState:UIControlStateSelected];
    [item setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
    [item setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
    [item setTitleTextAttributes:textAttributes forState:UIControlStateDisabled];
    item.tintColor = tintColot;
}

+ (void)settingNavigationBarTheme:(UINavigationBar *)bar {
    UIColor *globalColor = UIColor.whiteColor;
    //导航栏颜色和标题
    [bar setShadowImage:[UIImage new]];
    [bar setBackgroundImage:[UIImage imageWithColor:globalColor] forBarMetrics:UIBarMetricsDefault];
    [bar setTranslucent:NO];//设置导航不透明
    [bar setBarTintColor:globalColor];
    bar.titleTextAttributes = @{NSForegroundColorAttributeName : rgba(51, 51, 51, 1),
                                NSFontAttributeName : [UIFont systemFontOfSize:kAutoFont(17.0)
                                             weight : UIFontWeightMedium]};
}


//+ (void)initialize {
//     //设置导航栏主题
//    [self setupNavBarTheme];
//
//    //设置导航栏按钮主题
//    [self setupBarButtonItemTheme];
//}
/*
//设置导航栏主题
+ (void)setupNavBarTheme{
    UINavigationBar *navBar = [UINavigationBar appearance];

//    需要注释掉setBackgroundImage才起效
    [navBar setBackgroundImage:[UIImage imageNamed:navbar_whiteBg] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [navBar setShadowImage:[[UIImage alloc] init]];
//    navBar.backgroundColor = UIColor.whiteColor;
    
    
    //设置文字属性
    NSMutableDictionary *textArrs = [NSMutableDictionary dictionary];
    textArrs[NSFontAttributeName] = PF_Medium(18);
    textArrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#333333"];
    [navBar setTitleTextAttributes:textArrs];
}

//设置导航栏按钮主题
+ (void)setupBarButtonItemTheme
{
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    //设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = FONT(13);
    textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#333333"];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
    
    //设置文字不可点击时的属性
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSFontAttributeName] = FONT(13);
    disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#999999"];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
}*/

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    [self fullScreenSwipeGestures];
}

- (void)fullScreenSwipeGestures {
    
    // setp1:需要获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    
    // setp2:创建全屏滑动手势~调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    
    // step3:设置手势代理~拦截手势触发
    pan.delegate = self;
    
    // step4:别忘了~给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    
    // step5:将系统自带的滑动手势禁用
    self.interactivePopGestureRecognizer.enabled = NO;
    
    // steo6:还记得刚刚设置的代理吗？下面方法什么时候调用？在每次触发手势之前都会询问下代理，是否触发。
}

- (void)handleNavigationTransition:(UIPanGestureRecognizer *)pan{}

#pragma mark ========= 判断是否为根控制器 ==========
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 只要不等于1就返回YES，说明此时具有滑动功能
    return self.childViewControllers.count != 1;
}

//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    if (self.viewControllers.count > 0) {
//        viewController.hidesBottomBarWhenPushed = YES;
//    }
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
//    backItem.title = @"";
//    viewController.navigationItem.backBarButtonItem = backItem;
//    [super pushViewController:viewController animated:animated];
//}

//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    if (self.viewControllers.count > 0) {
//        viewController.hidesBottomBarWhenPushed = YES;
//        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"nav_back" highImage:@"nav_back" target:self action:@selector(back)];
//    }
//    [super pushViewController:viewController animated:animated];
//}

#pragma mark ========= 拦截控制器的push操作 ==========
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count > 0) {
        // 判断当前控制器是否为根控制器，如果不是，就执行下列代码
        //UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick)];
        //[viewController.navigationItem setLeftBarButtonItem:leftItem animated:YES];

        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"nav_back" highImage:@"nav_back" target:self action:@selector(back)];

        // 隐藏下面的TabBar
        viewController.hidesBottomBarWhenPushed = YES;
    }else{
        viewController.hidesBottomBarWhenPushed = NO;
    }

    // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    [self popViewControllerAnimated:YES];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *vc = [super popViewControllerAnimated:animated];
    return vc;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIViewController *topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}


@end
